package org.nanquan.dotjava.tags;

import java.util.Enumeration;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.taglib.tiles.util.TagUtils;

public class MethodParameter extends TagSupport  {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4210998472446051452L;

	MethodTag parentTag;
	
	private Object name;
	
	private Object property;
	
	private String value;
	
	private String type;

	

	@Override
	public int doAfterBody() throws JspException {
		// TODO 自动生成方法存根
		
		System.out.println("MethodParameter.doAfterBody");
		return super.doAfterBody();
	}

	@Override
	public int doEndTag() throws JspException {
		// TODO 自动生成方法存根
		//System.out.println("MethodParameter.doEndTag");
		Object obj=value;
		Class<?> paramtype=null;
		if(value==null)
		{
			try {
				
				obj=property!=null?TagUtils.getProperty(TagUtils.findAttribute((String)name, this.pageContext), (String) this.property): TagUtils.findAttribute((String)name, this.pageContext);
				if(type!=null)
				{
					Class<?> parclass=Class.forName(type);
					paramtype=parclass;
					//obj=parclass.cast(obj);
				}
				
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
				System.err.println("the attribute value or name no null");
			} 
		}
		
		this.parentTag.addParameter(obj);
		this.parentTag.addParameTypes(paramtype);
		return super.doEndTag();
	}

	@Override
	public int doStartTag() throws JspException {
		// TODO 自动生成方法存根
		return super.doStartTag();
	}

	@Override
	public String getId() {
		// TODO 自动生成方法存根
		return super.getId();
	}

	@Override
	public Tag getParent() {
		// TODO 自动生成方法存根
		return super.getParent();
	}

	@Override
	public Object getValue(String arg0) {
		// TODO 自动生成方法存根
		return super.getValue(arg0);
	}

	@Override
	public Enumeration<?> getValues() {
		// TODO 自动生成方法存根
		return super.getValues();
	}

	@Override
	public void setId(String arg0) {
		// TODO 自动生成方法存根
		super.setId(arg0);
	}

	@Override
	public void setPageContext(PageContext arg0) {
		// TODO 自动生成方法存根
		super.setPageContext(arg0);
	}

	@Override
	public void setParent(Tag arg0) {
		// TODO 自动生成方法存根
		super.setParent(arg0);
		this.parentTag=(MethodTag) arg0;
	}

	@Override
	public void setValue(String arg0, Object arg1) {
		// TODO 自动生成方法存根
		super.setValue(arg0, arg1);
	}

	public Object getName() {
		return name;
	}

	public void setName(Object name) {
		this.name = name;
	}

	public MethodTag getParentTag() {
		return parentTag;
	}

	public void setParentTag(MethodTag parentTag) {
		this.parentTag = parentTag;
	}

	public Object getProperty() {
		return property;
	}

	public void setProperty(Object property) {
		this.property = property;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
}
